/**
 * 
 */
package com.vector.ding.web.service.common;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.vector.ding.commons.util.StringUtil;
import com.vector.ding.commons.util.collection.CollectionUtil;
import com.vector.ding.dao.mybatis.mapper.HjxxpfInfoMapper;
import com.vector.ding.dao.mybatis.model.HjxxpfInfo;
import com.vector.ding.dao.mybatis.model.HjxxpfInfoExample;

/**
 * @author vector
 *
 */
@Service
public class BearScoreService {

	@Autowired
	private HjxxpfInfoMapper bearScoreMapper;
	
	public HjxxpfInfo getOne(String companyName) {
		List<HjxxpfInfo> list = getList(companyName);
		return CollectionUtil.isEmpty(list) ? null : list.get(0);
	}
	
	public HjxxpfInfo getOne(String companyName, int belongYear) {
		if (StringUtil.isEmpty(companyName))
			return null;
		
		HjxxpfInfoExample example = new HjxxpfInfoExample();
		example.or()
			.andCompanyNameEqualTo(companyName)
			.andBelongYearEqualTo(belongYear);
		List<HjxxpfInfo> list = bearScoreMapper.selectByExample(example);
		return CollectionUtil.isEmpty(list) ? null : list.get(0);
	}

	public List<HjxxpfInfo> getList(String companyName) {
		if (StringUtil.isEmpty(companyName))
			return Collections.emptyList();
		
		HjxxpfInfoExample example = new HjxxpfInfoExample();
		example.or().andCompanyNameEqualTo(companyName);
		List<HjxxpfInfo> list = bearScoreMapper.selectByExample(example);
		if (CollectionUtil.isEmpty(list))
			return Collections.emptyList();
		return list;
	}
	
	public List<HjxxpfInfo> getList(String companyName, int pageIndex, int pageSize) {
		if (StringUtil.isEmpty(companyName))
			return Collections.emptyList();
		
		PageHelper.startPage(pageIndex, pageSize, true);
		HjxxpfInfoExample example = new HjxxpfInfoExample();
		example.or().andCompanyNameEqualTo(companyName);
		List<HjxxpfInfo> list = bearScoreMapper.selectByExample(example);
		if (CollectionUtil.isEmpty(list))
			return Collections.emptyList();
		return list;
	}
	
	public BigDecimal getScore(String companyName, int belongYear) {
		HjxxpfInfo info = getOne(companyName, belongYear);
		return info == null ? null : info.getBearGrade();
	}
	
	public BigDecimal getScore(String companyName) {
		List<HjxxpfInfo> list = getList(companyName);
		if (CollectionUtil.isEmpty(list))
			return new BigDecimal(0);
		
		BigDecimal score = new BigDecimal(0);
		for (HjxxpfInfo info: list) {
			score = score.add(info.getBearGrade());
		}
		return score;
	}
}
